package com.xie.o2o.controller.frontend;

import com.google.common.collect.Maps;
import com.xie.o2o.dto.ShopExecution;
import com.xie.o2o.entity.Area;
import com.xie.o2o.entity.Shop;
import com.xie.o2o.entity.ShopCategory;
import com.xie.o2o.service.AreaService;
import com.xie.o2o.service.ShopCategoryService;
import com.xie.o2o.service.ShopService;
import com.xie.o2o.util.HttpServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @create: xie
 */
@Controller
@RequestMapping("/frontend")
public class ShopListController {
    private final static Logger log = LoggerFactory.getLogger(ShopListController.class);
    @Autowired
    private ShopCategoryService shopCategoryService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private ShopService shopService;

    /**
     * 获取一级、二级分类店铺列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listshopspageinfo", method = RequestMethod.GET)
    public Map<String, Object> listShopsPageInfo(HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        //从前端请求获取parentId
        long parentId = HttpServletUtil.getLong(request, "parentId");
        List<ShopCategory> shopCategoryList;
        if (parentId != -1) {
            //如果存在parentId,则取出所有一级下二级列表
            try {
                ShopCategory shopCategory = new ShopCategory();
                ShopCategory parent = new ShopCategory();
                parent.setShopCategoryId(parentId);
                shopCategory.setParent(parent);
                shopCategoryList = shopCategoryService.getShopCategoryList(shopCategory);
            } catch (Exception e) {
                log.error("获取一级、二级店铺列表失败:"+e.toString());
                throw new RuntimeException("获取店铺列表失败");
            }
        } else {
            //不存在parentId,则取出所有一级列表
            try {
                shopCategoryList = shopCategoryService.getShopCategoryList(null);
            } catch (Exception e) {
                log.error("获取一级、二级店铺列表失败:"+e.toString());
                throw new RuntimeException("获取店铺列表失败");
            }
        }
        map.put("shopCategoryList", shopCategoryList);

        //获取区域信息
        List<Area> areaList = null;
        try {
            areaList = areaService.getAreaList();
            map.put("success", true);
            map.put("areaList", areaList);
        } catch (Exception e) {
            log.error("获取区域信息列表失败:"+e.toString());
            throw new RuntimeException("获取区域信息列表失败");
        }
        return map;
    }

    /**
     * 获取指定条件下的店铺列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listshops", method = RequestMethod.GET)
    public Map<String, Object> listShops(HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        //获取页码
        int pageIndex = HttpServletUtil.getInt(request, "pageIndex");
        //获取每页显示数量
        int pageSize = HttpServletUtil.getInt(request, "pageSize");
        if (pageIndex > -1 && pageSize > -1) {
            //尝试获取一级类别
            long parentId = HttpServletUtil.getLong(request, "parentId");
            //尝试获取选择的二级类别
            long shopCategoryId = HttpServletUtil.getLong(request, "shopCategoryId");
            //尝试获取区域id
            int areaId = HttpServletUtil.getInt(request, "areaId");
            //尝试获取模糊查询名
            String shopName = HttpServletUtil.getString(request, "shopName");
            //根据条件组成获取店铺的信息实体类
            Shop shop = new Shop();
            Area area = new Area();
            ShopCategory childShopCategory = new ShopCategory();
            ShopCategory parentShopCategory = new ShopCategory();
            //区域信息
            if (areaId != -1) {
                area.setAreaId(areaId);
                shop.setArea(area);
            }
            //一级分类下面的二级店铺
            if (parentId != -1) {
                parentShopCategory.setShopCategoryId(parentId);
                childShopCategory.setParent(parentShopCategory);
                shop.setShopCategory(childShopCategory);
            }
            // 查询属于某个分类的店铺列表
            if (shopCategoryId != -1L) {
                ShopCategory shopCategory = new ShopCategory();
                shopCategory.setShopCategoryId(shopCategoryId);
                shop.setShopCategory(shopCategory);
            }
            //模糊查询
            if (shopName != null) {
                shop.setShopName(shopName);
            }
            //必须是审核通过的店铺
            shop.setEnableStatus(1);
            //根据条件查询店铺
            ShopExecution shopList = shopService.getShopList(shop, pageIndex, pageSize);
            map.put("shopList", shopList.getShopList());
            map.put("count", shopList.getCount());
            map.put("success", true);
        } else {
            map.put("success", false);
            map.put("errMsg", "empty pageSize or pageIndex");
        }
        return map;

    }
}
